-- possible models
-- small: models/props_junk/propane_tank001a.mdl
-- medium: models/props_c17/oildrum001.mdl
-- large: models/props_c17/canister_propane01a.mdl
-- fucking massive: models/props_c17/gravestone_cross001b.mdl

-- possible settings:
-- SITP_DPS
-- SITP_Range
-- SITP_MuzzleOffset
-- SITP_Mass

TOOL.Category		= "Construction"
TOOL.Name			= "#SITP Mining Tool"
TOOL.Command		= nil
TOOL.ConfigName		= ""

TOOL.ClientConVar[ "type" ] = "small"

cleanup.Register ( "sitp_miningtool" )

miningtools		= {}
miningids 		= {}

-- SMALL MINING BEAM
miningtools["small"] = {}
	miningtools["small"].Model			=	"models/props_junk/propane_tank001a.mdl"
	miningtools["small"].Mass			=	100
	miningtools["small"].MuzzleOffset	=	Vector(0, 0, 14)
	miningtools["small"].Range 			=	150
	miningtools["small"].DPS			=	10
	miningtools["small"].id				=	"small"
	
table.insert(miningids, miningtools["small"].id)

-- MEDIUM MINING BEAM
miningtools["medium"] = {}
	miningtools["medium"].Model			=	"models/props_c17/oildrum001.mdl"
	miningtools["medium"].Mass			=	500
	miningtools["medium"].MuzzleOffset	=	Vector(0, 0, 45)
	miningtools["medium"].Range 			=	300
	miningtools["medium"].DPS			=	25
	miningtools["medium"].id				=	"medium"
	
table.insert(miningids, miningtools["medium"].id)

-- LARGE MINING BEAM
miningtools["large"] = {}
	miningtools["large"].Model			=	"models/props_c17/canister_propane01a.mdl"
	miningtools["large"].Mass			=	1000
	miningtools["large"].MuzzleOffset	=	Vector(0, 0, 65)
	miningtools["large"].Range 			=	450
	miningtools["large"].DPS			=	50
	miningtools["large"].id				=	"large"
	
table.insert(miningids, miningtools["large"].id)

-- HUGE MINING BEAM
miningtools["huge"] = {}
	miningtools["huge"].Model			=	"models/props_c17/gravestone_cross001b.mdl"
	miningtools["huge"].Mass			=	4000
	miningtools["huge"].MuzzleOffset	=	Vector(0, 0, 60)
	miningtools["huge"].Range 			=	600
	miningtools["huge"].DPS				=	100
	miningtools["huge"].id				=	"huge"
	
table.insert(miningids, miningtools["huge"].id)

if CLIENT then
	language.Add( "Tool_sitpmining_name", "Space Is The Place - Mining Tool" )
	language.Add( "Tool_sitpmining_desc", "Spawn a Mining Tool")
	language.Add( "Tool_sitpmining_0", "Left click to spawn the SITP Mining Tool Entity" )
	
	language.Add( "Undone_sitp_miningtool", "Undone SITP Mining Tool" )
	language.Add( "SBoxLimit_sitp_miningtool", "You've reached the SITP Mining Tool limit!" )
	language.Add("MiningTool_Type", "Type of Mining Tool")
	
	function TOOL.BuildCPanel( CPanel )
		CPanel:ClearControls()

		CPanel:AddControl( "Header", { Text = "Space Is The Place Mining Tool", Description = "Creates mining tools" } )
		CPanel:AddControl( "Label", { Text = "This tool creates Space Is The Place Mining Tools - You can mine asteroids for points." }  )
	
		local Options = { Default = { coretype = "small" } }
		local CVars = { "type" }
		CPanel:AddControl( "ComboBox", {Label = "#MiningTool_Type", MenuButton = "0", Options = list.Get( "SITP_MiningToolTypeList" ) } )
	
		CPanel:AddHeader()
		CPanel:AddDefaultControls()		
	end
	list.Set( "SITP_MiningToolTypeList", "Small Mining Laser", { sitpmining_type = "small" } )
	list.Set( "SITP_MiningToolTypeList", "Medium Mining Laser", { sitpmining_type = "medium" } )
	list.Set( "SITP_MiningToolTypeList", "Large Mining Laser", { sitpmining_type = "large" } )
	list.Set( "SITP_MiningToolTypeList", "Huge Mining Laser", { sitpmining_type = "huge" } )
end

if SERVER then
	AddCSLuaFile("sitptool.lua")
	CreateConVar('sbox_maxsitp_miningtool', 10)
	
	function SITP_MakeMiningTool ( ply, Ang, SpawnPos, Model, Mass, MuzzleOffset, Range, DPS )
		if( !ply:CheckLimit( "sitp_miningtool" ) ) then return nil end
		
		local ent = ents.Create ( "sitp_miningtool" )
		ent.Model = Model
		ent.Mass = Mass
		ent.MuzzleOffset = MuzzleOffset
		ent.Range = Range
		ent.DPS = DPS
		
		ent:SetPos(SpawnPos)
		ent:SetAngles(Ang)
		ent.Owner = ply
		
		ent:Spawn()
		
		ply:AddCount( "sitp_miningtool" , ent )
		
		return ent
	end
	
	duplicator.RegisterEntityClass( "sitp_miningtool", SITP_MakeMiningTool, "Ang", "Pos", "Model", "Mass", "MuzzleOffset", "Range", "DPS")
end

function TOOL:LeftClick ( trace )
	if CLIENT then return true end
	
	local ply = self:GetOwner()
	local minetype = self:GetClientInfo( "type" )
	
	if(!(table.HasValue(miningids, minetype))) then
		return false
	end
	local gettool = miningtools[minetype]
	
	local SpawnPos = trace.HitPos + trace.HitNormal * 64
	local Ang = trace.HitNormal:Angle()
	Ang.pitch = Ang.pitch + 90
	
	local Model = gettool.Model
	local Mass = gettool.Mass
	local MuzzleOffset = gettool.MuzzleOffset
	local Range = gettool.Range
	local DPS = gettool.DPS
	
	local ent = SITP_MakeMiningTool ( ply, Ang, SpawnPos, Model, Mass, MuzzleOffset, Range, DPS )
	
	undo.Create("sitp_miningtool")
		undo.AddEntity ( ent )
		undo.SetPlayer ( ply )
	undo.Finish()
	ply:AddCleanup( "sitp_miningtool", ent )
end

function TOOL:RightClick ( trace )

end

function TOOL:Reload ( trace )

end

function TOOL:UpdateGhostMiningTool( ent, player )
	if (!ent) then return end
	if (!ent:IsValid() ) then return end
	
	local tr = utilx.GetPlayerTrace( player, player:GetCursorAimVector() )
	local trace = util.TraceLine( tr )
	if (!trace.Hit) then return end
	
	local Ang = trace.HitNormal:Angle()
	Ang.pitch = Ang.pitch + 90
	ent:SetPos( trace.HitPos + trace.HitNormal * 64 )
	ent:SetAngles( Ang )
	ent:SetNoDraw( false )
end

function TOOL:Think()
	local miningtype = self:GetClientInfo("type")
	if(!(table.HasValue(miningids, miningtype))) then return end
	
	if (!self.GhostEntity || !self.GhostEntity:IsValid() || self.GhostEntity:GetModel() != miningtools[miningtype].Model) then
        self:MakeGhostEntity( miningtools[miningtype].Model, Vector(0,0,0), Angle(0,0,0) )
    end
	   
	self:UpdateGhostMiningTool( self.GhostEntity, self:GetOwner() )
end